#ifndef __TAMAGAWA_H__
#define __TAMAGAWA_H__

#include <stdint.h>

typedef enum
{
    DataReadOut_ID0 = 0x02,       /* read abs 0 ~ 2 */
    DataReadOut_ID1 = 0x8A,       /* read abm 0 ~ 2 */
    DataReadOut_ID2 = 0x92,       /* read enid */
    DataReadOut_ID3 = 0x1A,       /* read all */
    WriteToEEPROM_ID6 = 0x32,     /* write one byte to eeprom */
    ReadOutFromEEPROM_IDD = 0xEA, /* read one byte from eeprom */
    Reset_ID7 = 0xBA,             /* reset and response abs 0 ~ 2 */
    Reset_ID8 = 0xCA,             /* reset and response abs 0 ~ 2 */
    Reset_IDC = 0x6A              /* reset and response abs 0 ~ 2 */
} tamagawa_cf_enum_type;          /* LSB FIRST */

typedef union {
    uint8_t data;
    uint8_t dd0 : 1;       /* fixed to "0" */
    uint8_t dd1 : 1;       /* fixed to "0" */
    uint8_t dd2 : 1;       /* fixed to "0" */
    uint8_t dd3 : 1;       /* fixed to "0" */
    uint8_t ea0 : 1;       /* Counting error */
    uint8_t ea1 : 1;       /* Logic-OR of Over-heat,ulti-turnerror, Battery error and Battery alarm istransmitted. */
    uint8_t ca0 : 1;       /* Logic "1" is transmitted whenParity error in Request frameOccurs.
                             Parity bit in Request Frame islocated in cc4 of Data ID code.*/
    uint8_t ca1 : 1;       /* Logic "1" is transmittedwhen Delimiter error inRequest frame occurs. */
} tamagawa_sf_struct_type; /*  */

typedef union {
    uint8_t data;
    uint8_t overspeed : 1;            /*  */
    uint8_t full_absolute_status : 1; /*  */
    uint8_t counting_error : 1;       /*  */
    uint8_t counter_overflow : 1;     /*  */
    uint8_t overheat : 1;             /*  */
    uint8_t multiturn_error : 1;      /*  */
    uint8_t battery_error : 1;        /*  */
    uint8_t battery_alarm : 1;        /*  */
} tamagawa_almc_struct_type;          /*  */
typedef union {
    uint8_t frame[4]; /*  */
    struct
    {
        tamagawa_cf_enum_type cf; /*  */
    } DataReadOut;                /*  */
    struct
    {
        tamagawa_cf_enum_type cf; /*  */
        uint8_t addr;             /*  */
        uint8_t data;             /*  */
        uint8_t crc;              /*  */
    } WriteToEEPROM;              /*  */
    struct
    {
        tamagawa_cf_enum_type cf; /*  */
        uint8_t data;             /*  */
        uint8_t crc;              /*  */
    } ReadFromEEPROM;             /*  */

} tamagawa_request_frame_struct_type; /*  */
typedef union {
    uint8_t frame[11]; /*  */
    struct
    {
        tamagawa_cf_enum_type cf;   /*  */
        tamagawa_sf_struct_type sf; /*  */
        uint8_t abs[3];             /*  */
        uint8_t crc;                /*  */
    } Data_ID0;                     /*  */
    struct
    {
        tamagawa_cf_enum_type cf;   /*  */
        tamagawa_sf_struct_type sf; /*  */
        uint8_t abm[3];             /*  */
        uint8_t crc;                /*  */
    } Data_ID1;                     /*  */
    struct
    {
        tamagawa_cf_enum_type cf;   /*  */
        tamagawa_sf_struct_type sf; /*  */
        uint8_t enid;               /*  */
        uint8_t crc;                /*  */
    } Data_ID2;                     /*  */
    struct
    {
        tamagawa_cf_enum_type cf;       /*  */
        tamagawa_sf_struct_type sf;     /*  */
        uint8_t abs[3];                 /*  */
        uint8_t enid;                   /*  */
        uint8_t abm[3];                 /*  */
        tamagawa_almc_struct_type almc; /*  */
        uint8_t crc;                    /*  */
    } Data_ID3;                         /*  */
    struct
    {
        tamagawa_cf_enum_type cf;   /*  */
        tamagawa_sf_struct_type sf; /*  */
        uint8_t abs[3];             /*  */
        uint8_t crc;                /*  */
    } Data_ID7;                     /*  */
    struct
    {
        tamagawa_cf_enum_type cf;   /*  */
        tamagawa_sf_struct_type sf; /*  */
        uint8_t abs[3];             /*  */
        uint8_t crc;                /*  */
    } Data_ID8;                     /*  */
    struct
    {
        tamagawa_cf_enum_type cf;      /*  */
        tamagawa_sf_struct_type sf;    /*  */
        uint8_t abs[3];                /*  */
        uint8_t crc;                   /*  */
    } Data_IDC;                        /*  */
} tamagawa_response_frame_struct_type; /*  */

typedef struct
{
    uint32_t abs;                    /*  */
    uint32_t abm;                    /*  */
    union{
        uint32_t status;/*  */
        struct{
            tamagawa_almc_struct_type almc;  /*  */
            tamagawa_sf_struct_type sf;      /*  */
            uint8_t enid;                    /*  */
            uint8_t pading;
        };
    };
} tamagawa_encoder_data_struct_type; /*  */

int32_t tamagawa_encoder_process(uint8_t *buf, uint8_t len, tamagawa_encoder_data_struct_type *p);
#endif
